package com.kenliang.netty.rpc.spring;

import com.kenliang.netty.rpc.netty.MessageSendExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author KenLiang
 * @description:
 * @date Created in 3:50 PM 2019/3/21.
 */
@Data
@Slf4j
public class NettyRpcReference implements FactoryBean, InitializingBean {

    private String interfaceName;
    private String ipAddr;

    @Override
    public Object getObject()  {
        return MessageSendExecutor.getInstance().execute(getObjectType());
    }

    @Override
    public Class<?> getObjectType() {
        try {
            return this.getClass().getClassLoader().loadClass(interfaceName);
        } catch (ClassNotFoundException e) {
            log.error("NettyRpcReference 实例化 创建代理失败：", ExceptionUtils.getStackTrace(e));
        }
        return null;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet()  {
        MessageSendExecutor.getInstance().loadRpcClientLoader(ipAddr);
    }
}
